package 笔记.多线程;

import java.util.concurrent.CountDownLatch;

/**
 * @author CHL
 * @ClassName: CountDownDemo
 * @Description: 多线程 减法计数器的使用
 * @date 2023/11/3 14:44
 * @Version 1.0
 */
public class CountDownDemo {
    public static void main(String[] args) {
        /*
        *   CountDownLatch为一次性计数器
        *   await阻塞线程，等所有线程都执行完后，再一起放行。
        *   底层也是用链表节点来记录执行的次数，当计数器值为0时，一起放行线程
        * */
        CountDownLatch countDownLatch = new CountDownLatch(8);
        for (int i = 0; i < 8; i++) {
            int finalI = i;
            new Thread(() -> {
                countDownLatch.countDown();
                System.out.println(Thread.currentThread().getName() + " 第:" + finalI + "颗龙珠");
            }).start();
        }


        try {
            /*
            *
            * */
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }




    }
}
